<?php

namespace api\modules\v1\controllers\rbac;

use api\controllers\OnAuthController;
use api\modules\v1\forms\rbac\MenuForm;
use common\models\member\Member;
use common\models\rbac\Menu;
use common\helpers\CommonHelper;
use services\rbac\MenuService;
use Yii;


class MenuController extends OnAuthController
{
    /**
     * @var Menu
     */
    public $modelClass = Menu::class;

    public function beforeAction($action)
    {
        $res = parent::beforeAction($action); // TODO: Change the autogenerated stub

        if (Yii::$app->user->identity->member->super_type !== Member::SUPER_TYPE_SUPER_ADMIN) {
            CommonHelper::api(422, '无权限操作');
            return false;
        }
        return $res;
    }

    /**
     * @api {get} /v1/rbac/menu/list 菜单列表
     * @apiName list
     * @apiGroup RbacMenu
     * @apiUse CommonParam
     *
     * @apiParam {Number} page                           页码
     * @apiParam {Number} pageSize                       每页显示数量
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.page                   当前页码
     * @apiSuccess {Number}  data.pageSize               每页显示数
     * @apiSuccess {Number}  data.total                  总数
     * @apiSuccess {Array}   data.list                   列表
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        // 查询结果
        $menuService = new MenuService();
        $data = $menuService->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化
        $data['list'] = $menuService->getListPageFormat($data['list']);

        return $data;
    }

    /**
     * @api {get} /v1/rbac/menu/info 菜单详情
     * @apiName info
     * @apiGroup RbacMenu
     * @apiUse CommonParam
     *
     * @apiParam {Number} id 菜单ID【新增ID不传，编辑必传ID】
     *
     * @apiSuccess {Object} data                    返回数据
     * @apiSuccess {Number} data.id                 菜单ID
     * @apiSuccess {Number} data.parent_menu_id     父菜单ID
     * @apiSuccess {Number} data.module_id          模块ID
     * @apiSuccess {String} data.name               菜单名称
     * @apiSuccess {String} data.url                菜单Url
     * @apiSuccess {String} data.class_style        菜单icon
     * @apiSuccess {Number} data.status             菜单状态
     * @apiSuccess {Number} data.sort               菜单排序
     * @apiSuccess {Array}  data.module_list        全部模块列表【循环】
     * @apiSuccess {Number} data.module_list.id     模块ID
     * @apiSuccess {String} data.module_list.name   模块名
     * @apiSuccess {Array}  data.menu_list          全部菜单列表【循环】
     * @apiSuccess {Number} data.menu_list.value    菜单ID
     * @apiSuccess {String} data.menu_list.title    菜单名
     * @apiSuccess {String} data.menu_list.checked  是否选中【true or false】
     * @apiSuccess {String} data.menu_list.expand   是否展开【true or false】
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        // 查询结果
        $menuService = new MenuService();
        return $menuService->getInfo((int)$id);
    }

    /**
     * @api {post} /v1/rbac/menu/create 新增菜单
     * @apiName create
     * @apiGroup RbacMenu
     * @apiUse CommonParam
     *
     * @apiBody {String} name              菜单名称
     * @apiBody {Number} parent_menu_id    父菜单ID【选填，顶级传0】
     * @apiBody {String} url               菜单url
     * @apiBody {String} class_style       菜单icon
     * @apiBody {Number} sort              排序【选填，不能小于0】
     * @apiBody {Number} status            菜单状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         菜单ID
     */
    public function actionCreate()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['parent_menu_id'] = intval($params['parent_menu_id'] ?? 0);
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new MenuForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $menuService = new MenuService();
        return $menuService->create($model);
    }

    /**
     * @api {post} /v1/rbac/menu/update 编辑菜单
     * @apiName update
     * @apiGroup RbacMenu
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                菜单ID
     * @apiBody {String} name              菜单名称
     * @apiBody {Number} module_id         模块ID
     * @apiBody {Number} parent_menu_id    父菜单ID【选填，顶级传0】
     * @apiBody {String} url               菜单url
     * @apiBody {String} class_style       菜单icon
     * @apiBody {Number} sort              排序【选填，不能小于0】
     * @apiBody {Number} status            菜单状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         菜单ID
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['parent_menu_id'] = intval($params['parent_menu_id'] ?? 0);
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new MenuForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $menuService = new MenuService();
        return $menuService->edit($model);
    }

    /**
     * @api {post} /v1/rbac/menu/delete 删除菜单
     * @apiName delete
     * @apiGroup RbacMenu
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                菜单ID
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         菜单ID
     */
    public function actionDelete()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;

        $model = new MenuForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $menuService = new MenuService();
        return $menuService->delete($model);
    }

    /**
     * @api {post} /v1/rbac/menu/update-status 编辑菜单状态
     * @apiName update-status
     * @apiGroup RbacMenu
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                菜单ID
     * @apiBody {Number} status            菜单状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         菜单ID
     */
//    public function actionUpdateStatus()
//    {
//        $params = Yii::$app->request->post();
//        $params['member_id'] = Yii::$app->user->identity->member_id;
//
//        $model = new MenuForm();
//        $model->setScenario('edit_status');
//        $model->attributes = $params;
//        if (!$model->validate()) {
//            // 返回数据验证失败
//            return CommonHelper::api(422, $this->getError($model));
//        }
//
//        $menuService = new MenuService();
//        return $menuService->editStatus($model);
//    }
}
